<?php

/**
 * This is the model class for table "dividend".
 *
 * The followings are the available columns in table 'dividend':
 * @property integer $id
 * @property string $date
 * @property integer $companyID
 * @property double $money
 * @property integer $portfolionameID
 *
 * The followings are the available model relations:
 * @property Portfolioname $portfolioname
 * @property Company $company
 */
class Dividend extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @return Dividend the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'dividend';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('companyID, portfolionameID', 'required'),
            array('companyID, portfolionameID', 'numerical', 'integerOnly' => true),
            array('money', 'numerical'),
            array('date', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, date, companyID, money, portfolionameID', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'portfolioname' => array(self::BELONGS_TO, 'Portfolioname', 'portfolionameID'),
            'company' => array(self::BELONGS_TO, 'Company', 'companyID'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'date' => 'Date',
            'companyID' => 'Company',
            'money' => 'Money',
            'portfolionameID' => 'Portfolioname',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;
        $backup = $this->date;
        if (preg_match('/[0-3]\d\/[01]\d\/\d{4}/', $this->date)) {
            $this->date = Stock::uk_date_to_mysql_date($this->date);
        }
        $criteria->order = "date desc";
        $criteria->compare('id', $this->id);
        $criteria->compare('date', $this->date, true);
        $criteria->compare('companyID', $this->companyID);
        $criteria->compare('money', $this->money);
        $criteria->compare('portfolionameID', $this->portfolionameID);

        $dataProvider = new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
        $this->date = $backup;
        return $dataProvider;
    }

}